<?php 
require_once(dirname(__FILE__).'/../../../../../plugins/sfGuardPlugin/modules/sfGuardAuth/lib/BasesfGuardAuthActions.class.php');


class sfGuardAuthActions extends BasesfGuardAuthActions
{
  public function executeRegister($request)
  {
	$user = $this->getUser();
	$this->reg_form=new RegisterForm();

if ($request->isMethod('post'))
 {
	$this->reg_form->bind($request->getParameter('register'));
		
    if ($this->reg_form->isValid())
    {
		$criteria = new Criteria ();
		$criteria -> add ( PlayerPeer::NICK,$this->reg_form->getValue('user'), Criteria::EQUAL );				
		if(PlayerPeer::doSelect($criteria)!=null)
		{
			$this->getUser()->setFlash('error','Gracz o takim niku już istnieje.');

		}else{
		
		
		
		$sd = new sfGuardUser();
		$sd->setUsername($this->reg_form->getValue('user'));		
		$sd->setPassword($this->reg_form->getValue('password'));
		$sd->setIsActive(1);	
		$sd->save();		
		
		$sd = new Player();
		$sd->setNick($this->reg_form->getValue('user'));
		$sd->save();
		
		$this->getUser()->setFlash('notice','Rejestracja się udała.');
		
		$signinUrl = $user->getReferer(sfConfig::get('app_sf_guard_plugin_success_signin_url', '@homepage'));
       
  		return $this->redirect($signinUrl);
		
		}
	}
}	
	$user->setReferer($this->getContext()->getActionStack()->getSize() > 1 ? $request->getUri() : $request->getReferer());

      $module = sfConfig::get('sf_login_module');
      if ($this->getModuleName() != $module)
      {
        return $this->redirect($module.'/'.sfConfig::get('sf_login_action'));
      }

      $this->getResponse()->setStatusCode(401);
	  
	  $class = sfConfig::get('app_sf_guard_plugin_signin_form', 'sfGuardFormSignin');
    $this->form = new $class();
	
	$this->setTemplate('register');
	 
  }
  
  public function executeSignin($request)
  {
    $this->reg_form=new RegisterForm();
	
	$user = $this->getUser();
    if ($user->isAuthenticated())
    {
      return $this->redirect('@homepage');
    }

    if ($request->isXmlHttpRequest())
    {
      $this->getResponse()->setHeaderOnly(true);
      $this->getResponse()->setStatusCode(401);

      return sfView::NONE;
    }

    $class = sfConfig::get('app_sf_guard_plugin_signin_form', 'sfGuardFormSignin');
    $this->form = new $class();
	
    if ($request->isMethod('post'))
    {
      $this->form->bind($request->getParameter('signin'));
      if ($this->form->isValid())
      {
        $values = $this->form->getValues();
        $this->getUser()->signin($values['user'], array_key_exists('remember', $values) ? $values['remember'] : false);
		
		

        $signinUrl = $user->getReferer(sfConfig::get('app_sf_guard_plugin_success_signin_url', '@homepage'));
       
  		return $this->redirect($signinUrl);
      } else {
           
      }
    }
    else
    {
      // if we have been forwarded, then the referer is the current URL
      // if not, this is the referer of the current request
      $user->setReferer($this->getContext()->getActionStack()->getSize() > 1 ? $request->getUri() : $request->getReferer());

      $module = sfConfig::get('sf_login_module');
      if ($this->getModuleName() != $module)
      {
        return $this->redirect($module.'/'.sfConfig::get('sf_login_action'));
      }

      $this->getResponse()->setStatusCode(401);
    }
  }
  
  public function executeSignout($request)
  {
      if ($this->getUser()->isAuthenticated())
	  {
		$this->getUser()->signOut();
      }
    
	 $this->redirect('@homepage');
  }
  
  
}

?>